﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ChequeManagement.PL
{
    public partial class PayAndReceive : PL.Base.Base
    {
        public PayAndReceive()
        {
            InitializeComponent();
        }

        private void PayAndReceive_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'cMDBDataSet.PayAndReceive' table. You can move, or remove it, as needed.
            PayAndReceiveDataGridView.DataSource = readFromDataBase();

        }

        private void SaveAndExitButton_Click(object sender, EventArgs e)
        {
            try
            {
                savePayAndReceive();
                Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
        private void savePayAndReceive()
        {
            BLL.BLLPayAndReceiveFactory payFactory = new BLL.BLLPayAndReceiveFactory();
            try
            {
                BLL.BLLPayAndReceive pay = new BLL.BLLPayAndReceive();
                BLL.BLLDrawer drawer = new BLL.BLLDrawer();
                drawer.DrawerID = 1;
                pay.Date = DateTime.Parse(DateTextBox.Text);
                pay.Description = DescriptionTextBox.Text;
                pay.DrawerObj = drawer;
                pay.Type = BLL.DocumentType.Pay;
                
                
                payFactory.BeginTransaction();
                payFactory.Add(pay);
                payFactory.CommitTransaction();

            }
            catch (Exception ex)
            {
                payFactory.RollBackTransaction();
                MessageBox.Show(ex.Message);
                
            }
        }
        private void clearForm()
        {
            DateTextBox.Text = "";
            DescriptionTextBox.Text = "";
        }
        private void SaveAndNextButton_Click(object sender, EventArgs e)
        {
            try
            {
                savePayAndReceive();
                clearForm();
                PayAndReceiveDataGridView.DataSource = readFromDataBase();
                //Go to the bottom of the dataGrid
                this.PayAndReceiveDataGridView.CurrentCell = this.PayAndReceiveDataGridView[0, this.PayAndReceiveDataGridView.Rows.Count - 1];
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
        private DataTable readFromDataBase()
        {
            DataTable dt = new DataTable();
            BLL.BLLPayAndReceiveFactory payFactory = new BLL.BLLPayAndReceiveFactory();
            payFactory.BeginTransaction();
            payFactory.GetAll(dt);
            payFactory.CommitTransaction();
            return dt;
        }

        private void ReadButton_Click(object sender, EventArgs e)
        {
            try
            {
                PayAndReceiveDataGridView.DataSource = readFromDataBase();
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void deletePayAndReceive(int id)
        {
            BLL.BLLPayAndReceiveFactory payFactory = new BLL.BLLPayAndReceiveFactory();
            payFactory.BeginTransaction();
            payFactory.Remove(id);
            payFactory.CommitTransaction();
        }
        private void editPayAndReceive()
        {
            BLL.BLLPayAndReceiveFactory payFactory = new BLL.BLLPayAndReceiveFactory();
            BLL.BLLPayAndReceive pay = new BLL.BLLPayAndReceive();
            BLL.BLLDrawer drawer = new BLL.BLLDrawer();
            drawer.DrawerID = 1;
            pay.Date = DateTime.Parse(DateTextBox.Text);
            pay.Description = DescriptionTextBox.Text;
            pay.DrawerObj = drawer;
            pay.Type = BLL.DocumentType.Pay;
            pay.PayAndReceiveID =  Convert.ToInt16(this.PayAndReceiveDataGridView.CurrentRow.Cells["PayAndReceiveID"].Value);
           
            payFactory.BeginTransaction();
            payFactory.Edit(pay, pay.PayAndReceiveID);
            payFactory.CommitTransaction();
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                deletePayAndReceive((int)PayAndReceiveDataGridView.CurrentRow.Cells[0].Value);
                PayAndReceiveDataGridView.DataSource = readFromDataBase();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void EditButton_Click(object sender, EventArgs e)
        {
            try
            {
                editPayAndReceive();

                //save the current row index to restore to that index after reading from database
                int rowIndex = this.PayAndReceiveDataGridView.CurrentRow.Index;
                PayAndReceiveDataGridView.DataSource = readFromDataBase();
                //Restore the last index
                this.PayAndReceiveDataGridView.CurrentCell = this.PayAndReceiveDataGridView[0, rowIndex];

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void PayAndReceiveDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DateTextBox.Text = this.PayAndReceiveDataGridView.CurrentRow.Cells["Date"].Value.ToString();
            DescriptionTextBox.Text = this.PayAndReceiveDataGridView.CurrentRow.Cells["Description"].Value.ToString();
        }
    }
}
